package com.quanshu.exchange.ui.gui.message.chat.emoji;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.utils.AppUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.quanshu.exchange.ui.gui.message.chat.emoji.EmojiconPagerView.EMOJI_COLUMN;
import static com.quanshu.exchange.ui.gui.message.chat.emoji.EmojiconPagerView.EMOJI_ROW;

/**
 * Created by jye on 2018/1/25.
 */
public class EmojiUtil {

    public static final String DELETE_KEY = "delete_icon";

    public static List<EmojiBean> getEmojiDatas(Context context) {
        String jsonString = AppUtils.getJson(context, "emoji.json");
        Type type = new TypeToken<Map<String, EmojiBean>>() {
        }.getType();
        Map<String, EmojiBean> map = new Gson().fromJson(jsonString, type);

        List<EmojiBean> beans = new ArrayList<>();
        EmojiBean emojiBean = new EmojiBean();
        emojiBean.setIcon(R.drawable.delete_expression);
        emojiBean.setEmojiText(DELETE_KEY);
        if (map != null && map.size() > 0) {
            int i = 0;
            int j = (EMOJI_ROW * EMOJI_COLUMN) - 1;
            for (Map.Entry<String, EmojiBean> entry : map.entrySet()) {
                if (i > 0 && i % j == 0) {
                    beans.add(emojiBean);
                }
                i += 1;
                beans.add(entry.getValue());
            }
            EmojiBean last = beans.get(beans.size() - 1);
            if (last.getEmojiText() != DELETE_KEY) {
                beans.add(emojiBean);
            }
        }

        return beans;
    }
}
